package week6.day36;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author fengzihao
 * @Date 2024-05-20 18:59
 */
public class MostFrequentWordAndNotBanned {
    public static void main(String[] args) {
        String paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
        String[] banned = new String[]{"hit"};
        String theMostFrequentWordAndNotBanned = getTheMostFrequentWordAndNotBanned(paragraph, banned);
        System.out.println("theMostFrequentWordAndNotBanned = " + theMostFrequentWordAndNotBanned);
    }

    private static String getTheMostFrequentWordAndNotBanned(String paragraph, String[] banned) {
        HashMap<String, Integer> wordCountsHashMap = new HashMap<>();
        String[] words = paragraph.split("[\\s,.]+");
        for (String word : words) {
            wordCountsHashMap.put(word.toLowerCase(),wordCountsHashMap.getOrDefault(word.toLowerCase(),0)+1);
        }
        for (String string : banned) {
            wordCountsHashMap.put(string.toLowerCase(),0);
        }
        Set<Map.Entry<String, Integer>> entries = wordCountsHashMap.entrySet();
        String result = "";
        int maxCount = 0;
        for (Map.Entry<String, Integer> entry : entries) {
            if (entry.getValue()>maxCount){
                maxCount = entry.getValue();
                result = entry.getKey();
            }
        }
        return result;
    }
}

